package java网络编程;

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author coderFu
 * @create 2022/2/25
 * TCP 通信编程
 * 网络相关API 都在 java.net包
 */
public class NetWorkTCP {


    public static void main(String[] args) throws IOException {
        String data = "This is data";

        //创建服务端并指定接口
        ServerSocket serverSocket = new ServerSocket(8088);
        System.out.println("服务端已经打开，客户端可以链接了......");

        boolean accept = true;
        // 无限循环 等待接收客户端的消息
        while (accept){
            Socket client = serverSocket.accept();;
            System.out.println("当前链接的客户端对象为："+client.getInetAddress());

            // 打印输出流
            PrintStream printStream = new PrintStream(client.getOutputStream());
            // 打印流中写入数据，发送给客户端
            printStream.println(data);
            // 关闭打印流
            printStream.close();
        }
        serverSocket.close();
    }



}
